package club_service

import (
	"blog/app/models/club_model"
	"blog/utils/tools"
	"encoding/json"
	"fmt"
	AnxiongLog "log"
)

// TableName 操作处理 club 的 tag 数据
const TableName = "tag"

// SaveCsvDataToEs 读取csv数据插入es ---- ok
func SaveCsvDataToEs(fileName string) {
	// 获取 csv 数据
	//fileName := "tag_202407191453.csv"
	headList, dataList := tools.ReadCsv("runtime/" + fileName)
	AnxiongLog.Println(dataList)
	// 把数据格式化为 json 数据
	for i, d := range dataList {
		jsItem := make(map[string]interface{})
		for ic, c := range d {
			jsItem[headList[ic]] = c
		}
		// map 转 json
		jsonItemData, err := json.Marshal(jsItem)
		if err != nil {
			AnxiongLog.Println("错误的json格式,excel行数", i)
			continue
		}
		fmt.Println(string(jsonItemData))
		// 把数据插入es
		err = tools.AddJson(string(jsonItemData), TableName)
		if err != nil {
			AnxiongLog.Println("把数据插入es", err)
			continue
		}
		fmt.Println("插入成功", string(jsonItemData))
	}
}

// ShowEsData 显示es数据 -- ok
func ShowEsData() {
	var data []club_model.ClubTagModel
	res, num := tools.ShowList(TableName)
	if num > 0 {
		for _, item := range res {
			var clubModel club_model.ClubTagModel
			fmt.Println(string(item.Source))
			err := json.Unmarshal(item.Source, &clubModel)
			if err != nil {
				AnxiongLog.Println("解析数据失败", err)
				continue
			}
			AnxiongLog.Println("显示数据", clubModel.CreatedByName)
			data = append(data, clubModel)
		}
	}
	fmt.Println(data)
}
